package cn.demoncat.util.pay.yeepay.entity;

import cn.demoncat.util.lang.constant.StringConstant;
import cn.demoncat.util.pay.yeepay.constant.TradeRefundStatus;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 回调参数-申请退款
 *
 * @author 延晓磊
 * @since 2020年10月19日
 */
public class TradeRefundNotify {

    // 收款商户编号
    private String merchantNo;
    // 订单编号（对应交易订单）
    private String orderId;
    // 易宝收款订单号（对应交易订单）
    private String uniqueOrderNo;
    // 订单金额（元）
    private BigDecimal orderAmount;
    // 退款申请号
    private String refundRequestId;
    // 易宝退款单号
    private String uniqueRefundNo;
    // 退款金额（元）
    private BigDecimal refundAmount;
    // 退款请求时间
    private LocalDateTime refundRequestDate;
    // 退款状态：TradeRefundStatus.name()
    private String status;
    // 退款成功时间（成功时返回）
    private LocalDateTime refundSuccessDate;
    // 退款失败原因（失败时返回）
    private String errorMessage;

    /**
     * 查询退款状态
     *
     * @author 延晓磊
     * @since 2020年10月19日
     */
    public TradeRefundStatus status() {
        if (this.status != null) {
            return TradeRefundStatus.valueOf(this.status);
        }
        return null;
    }

    /**
     * 错误消息
     *
     * @return 空表示成功，非空表示失败
     *
     * @author 延晓磊
     * @since 2021年03月02日
     */
    public String getError(){
        TradeRefundStatus status = status();
        if (TradeRefundStatus.SUCCESS.equals(status)) {
            return StringConstant.EMPTY;
        }else{
            return StringUtils.isBlank(errorMessage) ? errorMessage : (status == null ? "未知原因" : status.getName());
        }
    }

    public String getMerchantNo() {
        return merchantNo;
    }

    public void setMerchantNo(String merchantNo) {
        this.merchantNo = merchantNo;
    }

    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    public String getRefundRequestId() {
        return refundRequestId;
    }

    public void setRefundRequestId(String refundRequestId) {
        this.refundRequestId = refundRequestId;
    }

    public String getUniqueRefundNo() {
        return uniqueRefundNo;
    }

    public void setUniqueRefundNo(String uniqueRefundNo) {
        this.uniqueRefundNo = uniqueRefundNo;
    }

    public BigDecimal getRefundAmount() {
        return refundAmount;
    }

    public void setRefundAmount(BigDecimal refundAmount) {
        this.refundAmount = refundAmount;
    }

    public LocalDateTime getRefundRequestDate() {
        return refundRequestDate;
    }

    public void setRefundRequestDate(LocalDateTime refundRequestDate) {
        this.refundRequestDate = refundRequestDate;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getUniqueOrderNo() {
        return uniqueOrderNo;
    }

    public void setUniqueOrderNo(String uniqueOrderNo) {
        this.uniqueOrderNo = uniqueOrderNo;
    }

    public BigDecimal getOrderAmount() {
        return orderAmount;
    }

    public void setOrderAmount(BigDecimal orderAmount) {
        this.orderAmount = orderAmount;
    }

    public LocalDateTime getRefundSuccessDate() {
        return refundSuccessDate;
    }

    public void setRefundSuccessDate(LocalDateTime refundSuccessDate) {
        this.refundSuccessDate = refundSuccessDate;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }
}
